package com.onlyxiahui.framework.action.dispatcher.general.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.core.MethodParameter;

import com.onlyxiahui.framework.action.dispatcher.ActionContext;
import com.onlyxiahui.framework.action.dispatcher.extend.ActionRequest;
import com.onlyxiahui.framework.action.dispatcher.extend.ActionResponse;
import com.onlyxiahui.framework.action.dispatcher.extend.ArgumentBox;
import com.onlyxiahui.framework.action.dispatcher.extend.MethodArgumentResolver;

/**
 * Date 2019-01-12 21:58:45<br>
 * Description
 * 
 * @author XiaHui<br>
 * @since 1.0.0
 */

public class GeneralArgumentActionResolver implements MethodArgumentResolver {

	List<Class<?>> list = new ArrayList<>();

	public GeneralArgumentActionResolver() {
		list.add(ActionContext.class);
		list.add(ActionRequest.class);
		list.add(ActionResponse.class);
		list.add(ArgumentBox.class);
	}

	@Override
	public boolean supportsParameter(MethodParameter parameter) {
		boolean support = false;
		Class<?> clazz = parameter.getParameterType();
		support = list.contains(clazz);
		return support;
	}

	@Override
	public Object resolveArgument(ActionContext actionContext, MethodParameter parameter, ActionRequest request, ActionResponse response, ArgumentBox argumentBox) {
		Object object = null;
		Class<?> clazz = parameter.getParameterType();
		if (ActionContext.class == clazz) {
			object = actionContext;
		} else if (ActionRequest.class == clazz) {
			object = request;
		} else if (ActionResponse.class == clazz) {
			object = response;
		} else if (ArgumentBox.class == clazz) {
			object = argumentBox;
		}
		return object;
	}
}
